package com.ds.bar;

import com.ds.bpm.formula.ParticipantSelect;
import com.ds.common.JDSException;
import com.ds.enums.attribute.Attributetype;
import com.ds.esb.config.formula.EngineType;
import com.ds.esb.config.formula.FormulaType;
import com.ds.esd.bpm.BPMFactory;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.tree.annotation.TreeAnnotation;
import com.ds.esd.tool.ui.component.list.TreeListItem;

import java.util.HashMap;
import java.util.List;

@TreeAnnotation
public class FormulaTypeBar extends TreeListItem {


    public FormulaTypeBar() {
        this.caption = "公式类型";
        this.setId("allFormulaType");
        this.setIniFold(false);
        EngineType[] types = EngineType.values();
        for (EngineType type : types) {
            FormulaTypeBar engineType = new FormulaTypeBar(type);
            if (engineType.getSub() != null && engineType.getSub().size() > 0) {
                this.addChild(engineType);
            }

        }
    }

    public FormulaTypeBar(Attributetype baseType) {
        this.caption = baseType.getName();
        this.setId(baseType.getType());
        this.setIniFold(false);
        this.setImageClass(baseType.getImageClass());
        FormulaType[] formulaTypes = FormulaType.values();
        for (FormulaType formulaType : formulaTypes) {
            if (formulaType.getBaseType().equals(baseType)) {
                FormulaTypeBar formulaTypeItem = new FormulaTypeBar(formulaType);
                this.addChild(formulaTypeItem);

            }
        }
    }

    public FormulaTypeBar(FormulaType formulaType) {
        this.caption = formulaType.getName();
        this.setImageClass(formulaType.getImageClass());
        this.setId(formulaType.getType());
        try {
            List<ParticipantSelect> selects = BPMFactory.getInstance(ESDFacrory.getESDClient().getSpace()).getFormulas(formulaType);
            for (ParticipantSelect select : selects) {
                this.addChild(new FormulaTypeBar(select));
            }
        } catch (JDSException e) {
            e.printStackTrace();
        }


        this.setIniFold(false);
        this.setCloseBtn(true);
        this.setClassName("admin.formula.Formulas");
        this.tagVar = new HashMap<>();
        this.tagVar.put("formulaType", formulaType.getType());

    }

    public FormulaTypeBar(ParticipantSelect select) {
        this.caption = select.getSelectName();
        this.id = select.getParticipantSelectId();
        this.tips = select.getFormula();
        this.tagVar = new HashMap<>();
        this.setImageClass("spafont spa-icon-function");
        this.tagVar.put("formulaType", select.getFormulaType().getType());
        this.tagVar.put("selectId", select.getParticipantSelectId());

    }


    public FormulaTypeBar(EngineType engineType) {
        this.caption = engineType.getName();
        this.setImageClass(engineType.getImageClass());
        this.setId(engineType.getType());
        this.setIniFold(false);
        Attributetype[] attributetypes = Attributetype.values();

        for (Attributetype attributetype : attributetypes) {
            if (attributetype.getEngineType().equals(engineType)) {
                FormulaTypeBar formulaTypeItem = new FormulaTypeBar(attributetype);
                if (formulaTypeItem.getSub() != null && formulaTypeItem.getSub().size() > 0) {
                    this.addChild(formulaTypeItem);
                }
            }
        }

    }


}
